package cc.rengu.igas.channel.allinpay.core.service.trans;

import cc.rengu.igas.channel.allinpay.common.constant.AllinpayParamConstant;
import cc.rengu.igas.channel.allinpay.common.constant.AllinpayTreeNodeConstant;
import cc.rengu.igas.channel.allinpay.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.allinpay.core.service.base.AuthenticationService;
import cc.rengu.igas.channel.allinpay.facade.request.AllinpayFiveFactorAuthenticationRequest;
import cc.rengu.igas.channel.allinpay.facade.response.AllinpayFiveFactorAuthenticationResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 通联五要素鉴权
 *
 * @version 1.0.0
 * @date 2021.05.10
 */

public class FiveFactorAuthenticationService extends AuthenticationService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        AllinpayFiveFactorAuthenticationRequest allinpayFiveFactorAuthenticationRequest = new AllinpayFiveFactorAuthenticationRequest();
        ConvertUtil.convertOutput(allinpayFiveFactorAuthenticationRequest);
        if (StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getBankCode()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountAttrb()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountNo()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountName()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getIdType()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getId()) ||
                StringUtil.isEmptyOrNull(allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getTel())) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("缺少一个或多个业务参数！");
            throw new BizException(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL);
        }
        return allinpayFiveFactorAuthenticationRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        AllinpayFiveFactorAuthenticationRequest allinpayFiveFactorAuthenticationRequest = (AllinpayFiveFactorAuthenticationRequest) request;
        AllinpayFiveFactorAuthenticationResponse allinpayFiveFactorAuthenticationResponse = new AllinpayFiveFactorAuthenticationResponse();
        allinpayFiveFactorAuthenticationResponse.setHeader(allinpayFiveFactorAuthenticationRequest.getHeader());
        //通联调用接口赋值
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, allinpayFiveFactorAuthenticationRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.INST_ID, allinpayFiveFactorAuthenticationRequest.getHeader().getInstId());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.TRX_CODE, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getTrxCode());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.VERSION, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getVersion());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.DATA_TYPE, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getDataType());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.LEVEL, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getLevel());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.USER_NAME, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getUserName());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.USER_PASS, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getUserPass());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.REQ_SN, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getReqSn());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.SIGNED_MSG, allinpayFiveFactorAuthenticationRequest.getAllinpayInfo().getSignedMsg());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.MERCHANT_ID, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getMerchantId());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.SUBMIT_TIME, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getSubmitTime());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.ACCOUNT_ATTRB, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountAttrb());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.ACCOUNT_NO, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountNo());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.ACCOUNT_NAME, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getAccountName());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.ID_TYPE, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getIdType());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.ID, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getId());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.TEL, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getTel());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.BANK_CODE, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getBankCode());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.MERREM, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getMerrem());
        xmlTreeUtil.setXmlTreeStringValue(AllinpayTreeNodeConstant.REMARK, allinpayFiveFactorAuthenticationRequest.getSignFactorInfo().getRemark());
        //服务调用
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            /* 演示环境发送短信默认返回成功 */
            allinpayFiveFactorAuthenticationResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            allinpayFiveFactorAuthenticationResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            int iReturnCode = schedule.callService(AllinpayParamConstant.ALLINPAY_COMMUNICATION_SVC_NM, AllinpayParamConstant.ALLINPAY_COMMUNICATION_SVC_LABEL);
            if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                allinpayFiveFactorAuthenticationResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                allinpayFiveFactorAuthenticationResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                rglog.error("callService {} 失败！返回值={}", AllinpayParamConstant.ALLINPAY_COMMUNICATION_SVC_NM, iReturnCode);
                allinpayFiveFactorAuthenticationResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                allinpayFiveFactorAuthenticationResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } else {
                rglog.error("callService {} 失败！返回值={}", AllinpayParamConstant.ALLINPAY_COMMUNICATION_SVC_NM, iReturnCode);
                allinpayFiveFactorAuthenticationResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                allinpayFiveFactorAuthenticationResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            }
        }
        //调用结果返回
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(allinpayFiveFactorAuthenticationResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        AllinpayFiveFactorAuthenticationResponse allinpayFiveFactorAuthenticationResponse = (AllinpayFiveFactorAuthenticationResponse) response.getResult();
        ConvertUtil.convertInput(allinpayFiveFactorAuthenticationResponse);
    }
}
